<!DOCTYPE html>
<html lang="zh-CN,en,default">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|Amita:300,300italic,400,400italic,700,700italic|Montserrat:300,300italic,400,400italic,700,700italic|PT Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"dpbirder.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"flat"},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":true,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

<script>
    (function(d, w, c) {
        w.ChatraID = 'Z8KH5JTPhxGC84tKb';
        var s = d.createElement('script');
        w[c] = w[c] || function() {
            (w[c].q = w[c].q || []).push(arguments);
        };
        s.async = true;
        s.src = 'https://call.chatra.io/chatra.js';
        if (d.head) d.head.appendChild(s);
    })(document, window, 'Chatra');
</script>
  <meta name="description" content="题目汇总    题目 题解     739. 每日温度 https:&#x2F;&#x2F;leetcode.cn&#x2F;problems&#x2F;daily-temperatures&#x2F;solutions&#x2F;2271162&#x2F;dan-diao-zhan-ji-suan-xia-yi-ge-geng-da-so5k8&#x2F;   496.下一个更大元素 I https:&#x2F;&#x2F;leetcode.cn&#x2F;problems&#x2F;next-greater-">
<meta property="og:type" content="article">
<meta property="og:title" content="单调栈相关题目">
<meta property="og:url" content="http://dpbirder.gitee.io/2023/05/19/%E5%8D%95%E8%B0%83%E6%A0%88%E7%9B%B8%E5%85%B3%E9%A2%98%E7%9B%AE/index.html">
<meta property="og:site_name" content="dpBirder&#39;s Blog">
<meta property="og:description" content="题目汇总    题目 题解     739. 每日温度 https:&#x2F;&#x2F;leetcode.cn&#x2F;problems&#x2F;daily-temperatures&#x2F;solutions&#x2F;2271162&#x2F;dan-diao-zhan-ji-suan-xia-yi-ge-geng-da-so5k8&#x2F;   496.下一个更大元素 I https:&#x2F;&#x2F;leetcode.cn&#x2F;problems&#x2F;next-greater-">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230514171606312.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230512120002246.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230519172345474.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230520171345851.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/stack.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230519173358221.png">
<meta property="article:published_time" content="2023-05-19T09:24:43.000Z">
<meta property="article:modified_time" content="2023-05-21T14:36:24.700Z">
<meta property="article:author" content="dpbirder">
<meta property="article:tag" content="个人技术博客">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230514171606312.png">

<link rel="canonical" href="http://dpbirder.gitee.io/2023/05/19/%E5%8D%95%E8%B0%83%E6%A0%88%E7%9B%B8%E5%85%B3%E9%A2%98%E7%9B%AE/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<script> 
 (function(){
        if(''){
            if (prompt('请输入文章密码') !== ''){
                alert('密码错误！');
                history.back();
            }
        }
    })();
</script>
  <title>单调栈相关题目 | dpBirder's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">dpBirder's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">15</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">26</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">51</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/zhengzp2019" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://dpbirder.gitee.io/2023/05/19/%E5%8D%95%E8%B0%83%E6%A0%88%E7%9B%B8%E5%85%B3%E9%A2%98%E7%9B%AE/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/boy.jpg">
      <meta itemprop="name" content="dpbirder">
      <meta itemprop="description" content="目前研一就读于西安电子科技大学">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="dpBirder's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          单调栈相关题目
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2023-05-19 17:24:43" itemprop="dateCreated datePublished" datetime="2023-05-19T17:24:43+08:00">2023-05-19</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-05-21 22:36:24" itemprop="dateModified" datetime="2023-05-21T22:36:24+08:00">2023-05-21</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="url" rel="index"><span itemprop="name">数据结构</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%A0%88/" itemprop="url" rel="index"><span itemprop="name">栈</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>11k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>10 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="题目汇总"><a class="markdownIt-Anchor" href="#题目汇总"></a> 题目汇总</h2>
<table>
<thead>
<tr>
<th>题目</th>
<th>题解</th>
</tr>
</thead>
<tbody>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/daily-temperatures/">739. 每日温度</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/daily-temperatures/solutions/2271162/dan-diao-zhan-ji-suan-xia-yi-ge-geng-da-so5k8/">https://leetcode.cn/problems/daily-temperatures/solutions/2271162/dan-diao-zhan-ji-suan-xia-yi-ge-geng-da-so5k8/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/next-greater-element-i/">496.下一个更大元素 I</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/next-greater-element-i/solutions/2276292/dan-diao-zhan-jie-jue-xia-yi-ge-geng-da-b8od6/">https://leetcode.cn/problems/next-greater-element-i/solutions/2276292/dan-diao-zhan-jie-jue-xia-yi-ge-geng-da-b8od6/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/next-greater-element-ii/">503.下一个更大元素II</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/next-greater-element-ii/solutions/2276307/dan-diao-zhan-jie-jue-xia-yi-ge-geng-da-lcma5/">https://leetcode.cn/problems/next-greater-element-ii/solutions/2276307/dan-diao-zhan-jie-jue-xia-yi-ge-geng-da-lcma5/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/trapping-rain-water/">42.接雨水</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/trapping-rain-water/solutions/2278600/bao-li-dong-tai-gui-hua-shuang-zhi-zhen-l4gr1/">https://leetcode.cn/problems/trapping-rain-water/solutions/2278600/bao-li-dong-tai-gui-hua-shuang-zhi-zhen-l4gr1/</a></td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/largest-rectangle-in-histogram/">84.柱状图中最大的矩形</a></td>
<td><a target="_blank" rel="noopener" href="https://leetcode.cn/problems/largest-rectangle-in-histogram/solutions/2276667/bao-li-shuang-zhi-zhen-dan-diao-zhan-shi-xdw0/">https://leetcode.cn/problems/largest-rectangle-in-histogram/solutions/2276667/bao-li-shuang-zhi-zhen-dan-diao-zhan-shi-xdw0/</a></td>
</tr>
</tbody>
</table>
<a id="more"></a>
<h2 id="739-每日温度"><a class="markdownIt-Anchor" href="#739-每日温度"></a> 739. 每日温度</h2>
<h3 id="题目描述"><a class="markdownIt-Anchor" href="#题目描述"></a> 题目描述</h3>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230514171606312.png" alt="image-20230514171606312"></p>
<h3 id="题解"><a class="markdownIt-Anchor" href="#题解"></a> 题解</h3>
<h4 id="我的思路"><a class="markdownIt-Anchor" href="#我的思路"></a> 我的思路</h4>
<p>这题要求计算对于第i天，下一个更高温度出现几天后。可以抽象成计算元素i下一个更大元素出现的位置，对于这种问题可以使用单调栈解决。考虑如下问题：</p>
<p>1、栈中存储的元素是什么？</p>
<blockquote>
<p>单调栈里存储元素下标即可，若想知道具体的温度，可以使用T[i]获得</p>
</blockquote>
<p>2、单调栈的顺序是什么？</p>
<blockquote>
<p>因为这题要求的是下一个更大的元素，所以应该维护一个从栈底到栈顶递减的单调栈</p>
<p>若当前枚举元素比栈顶元素大，说明当前元素是栈顶元素下一个更大的元素；</p>
<p>若当前元素小于等于栈顶元素，说明栈顶元素下一个更小的元素还没出现，当前元素入栈</p>
</blockquote>
<p>3、考虑下面三种判断条件</p>
<blockquote>
<ul>
<li>当前元素于栈顶元素：当前元素入栈</li>
<li>当前元素等于栈顶元素：当前元素入栈</li>
<li>当前元素大于栈顶元素：当前元素是栈顶元素的下一个更大元素，出栈，处理下一个栈顶元素</li>
</ul>
</blockquote>
<h4 id="算法过程"><a class="markdownIt-Anchor" href="#算法过程"></a> 算法过程</h4>
<ul>
<li>从前向后枚举元素，并维护一个单调递减的单调栈</li>
<li>若当前元素大于栈顶元素
<ul>
<li>处理结果，出栈，继续比较</li>
</ul>
</li>
<li>当前元素入栈</li>
<li>枚举结束，返回结果</li>
</ul>
<h4 id="我的代码"><a class="markdownIt-Anchor" href="#我的代码"></a> 我的代码</h4>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 版本一：用分支语句分别考虑上述三种情况</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[] dailyTemperatures(<span class="keyword">int</span>[] temperatures) &#123;</span><br><span class="line">        <span class="keyword">int</span> hh = <span class="number">0</span>, n = temperatures.length;</span><br><span class="line">        <span class="comment">// 用数组模拟单调栈</span></span><br><span class="line">        <span class="keyword">int</span>[] stack = <span class="keyword">new</span> <span class="keyword">int</span>[n + <span class="number">10</span>];</span><br><span class="line">        <span class="keyword">int</span>[] ans = <span class="keyword">new</span> <span class="keyword">int</span>[temperatures.length];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="comment">// 情况一</span></span><br><span class="line">            <span class="keyword">if</span> (temperatures[stack[hh]] &gt; temperatures[i])</span><br><span class="line">                stack[++hh] = i;</span><br><span class="line">            <span class="comment">// 情况二</span></span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span> (temperatures[stack[hh]] == temperatures[i])</span><br><span class="line">                stack[++hh] = i;</span><br><span class="line">            <span class="comment">// 情况三</span></span><br><span class="line">            <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">while</span> (hh != <span class="number">0</span> &amp;&amp; temperatures[stack[hh]] &lt; temperatures[i]) &#123;</span><br><span class="line">                    <span class="keyword">int</span> id = stack[hh];</span><br><span class="line">                    hh--;</span><br><span class="line">                    ans[id] = i - id;</span><br><span class="line">                &#125;</span><br><span class="line">                stack[++hh] = i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 版本二：版本一精简之后的代码</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[] dailyTemperatures(<span class="keyword">int</span>[] temperatures) &#123;</span><br><span class="line">        <span class="keyword">int</span> hh = <span class="number">0</span>, n = temperatures.length;</span><br><span class="line">        <span class="comment">// 用数组模拟单调栈</span></span><br><span class="line">        <span class="keyword">int</span>[] stack = <span class="keyword">new</span> <span class="keyword">int</span>[n + <span class="number">10</span>];</span><br><span class="line">        <span class="keyword">int</span>[] ans = <span class="keyword">new</span> <span class="keyword">int</span>[temperatures.length];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">while</span> (hh != <span class="number">0</span> &amp;&amp; temperatures[stack[hh]] &lt; temperatures[i]) &#123;</span><br><span class="line">                <span class="keyword">int</span> index = stack[hh--];</span><br><span class="line">                ans[index] = i - index;</span><br><span class="line">            &#125;</span><br><span class="line">            stack[++hh] = i;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="496下一个更大元素i"><a class="markdownIt-Anchor" href="#496下一个更大元素i"></a> 496.下一个更大元素I</h2>
<h3 id="题目描述-2"><a class="markdownIt-Anchor" href="#题目描述-2"></a> 题目描述</h3>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230512120002246.png" alt="image-20230512120002246"></p>
<h3 id="题解-2"><a class="markdownIt-Anchor" href="#题解-2"></a> 题解</h3>
<h4 id="我的思路-2"><a class="markdownIt-Anchor" href="#我的思路-2"></a> 我的思路</h4>
<p>这题的思路与<a target="_blank" rel="noopener" href="https://leetcode.cn/problems/daily-temperatures/">739. 每日温度</a>思路基本一致都是计算下一个更大元素，只不过计算的是nums1中元素在nums2中下一个更大元素的位置(num1是nums2的子集)，可参考<a target="_blank" rel="noopener" href="https://leetcode.cn/problems/daily-temperatures/solutions/2271162/dan-diao-zhan-ji-suan-xia-yi-ge-geng-da-so5k8/">我的题解</a>计算下一个更大的元素。因为nums2和nums1中的元素是不重复的，所以可以用一个哈希表存储nums2中每个元素的下一个更大元素，然后枚举nums1中所有元素，使用哈希表可以直接获得每个元素下一个更大元素。</p>
<h4 id="算法过程-2"><a class="markdownIt-Anchor" href="#算法过程-2"></a> 算法过程</h4>
<ul>
<li>计算nums2中下一个元素更大元素，并维护一个哈希表</li>
<li>枚举nums1中元素，对于每个元素可以通过哈希表直接获取答案</li>
</ul>
<h4 id="我的代码-2"><a class="markdownIt-Anchor" href="#我的代码-2"></a> 我的代码</h4>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[] nextGreaterElement(<span class="keyword">int</span>[] nums1, <span class="keyword">int</span>[] nums2) &#123;</span><br><span class="line">        <span class="keyword">int</span> hh = <span class="number">0</span>, n = nums2.length;</span><br><span class="line">        <span class="keyword">int</span>[] stack = <span class="keyword">new</span> <span class="keyword">int</span>[n + <span class="number">10</span>];</span><br><span class="line">        Map&lt;Integer, Integer&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">while</span> (hh != <span class="number">0</span> &amp;&amp; nums2[stack[hh]] &lt; nums2[i]) &#123;</span><br><span class="line">                <span class="keyword">int</span> id = stack[hh];</span><br><span class="line">                map.put(nums2[id], nums2[i]);</span><br><span class="line">                hh--;</span><br><span class="line">            &#125;</span><br><span class="line">            stack[++hh] = i;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span>[] ans = <span class="keyword">new</span> <span class="keyword">int</span>[nums1.length];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; nums1.length; i++)&#123;</span><br><span class="line">            ans[i] = -<span class="number">1</span>;</span><br><span class="line">            <span class="keyword">if</span>(map.containsKey(nums1[i])) ans[i] = map.get(nums1[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="503下一个更大元素ii"><a class="markdownIt-Anchor" href="#503下一个更大元素ii"></a> 503.下一个更大元素II</h2>
<h3 id="题目描述-3"><a class="markdownIt-Anchor" href="#题目描述-3"></a> 题目描述</h3>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230519172345474.png" alt="image-20230519172345474"></p>
<h3 id="题解-3"><a class="markdownIt-Anchor" href="#题解-3"></a> 题解</h3>
<h4 id="我的思路-3"><a class="markdownIt-Anchor" href="#我的思路-3"></a> 我的思路</h4>
<p>这题与<a target="_blank" rel="noopener" href="https://leetcode.cn/problems/next-greater-element-i/">496.下一个更大元素 I</a>基本一致，只不过给定的是一个循环数组，所以在遍历数组的时候要模拟遍历一个循环数组，可以使用如下方式循环遍历一个数组。</p>
<h4 id="我的代码-3"><a class="markdownIt-Anchor" href="#我的代码-3"></a> 我的代码</h4>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span>[] nextGreaterElements(<span class="keyword">int</span>[] nums) &#123;</span><br><span class="line">        <span class="keyword">int</span> n = nums.length;</span><br><span class="line">        <span class="keyword">int</span>[] ans = <span class="keyword">new</span> <span class="keyword">int</span>[n];</span><br><span class="line">        <span class="keyword">int</span> hh = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span>[] stack = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">2</span> * n + <span class="number">5</span>];</span><br><span class="line">        Arrays.fill(ans, -<span class="number">1</span>);</span><br><span class="line">        <span class="comment">// 用一个循环变量循环遍历数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">2</span> * n - <span class="number">1</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">while</span> (hh != <span class="number">0</span> &amp;&amp; nums[stack[hh]] &lt; nums[i % n]) &#123;</span><br><span class="line">                <span class="keyword">int</span> id = stack[hh];</span><br><span class="line">                hh--;</span><br><span class="line">                ans[id] = nums[i % n];</span><br><span class="line">            &#125;</span><br><span class="line">            stack[++hh] = i % n;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="42-接雨水"><a class="markdownIt-Anchor" href="#42-接雨水"></a> 42. 接雨水</h2>
<h3 id="题目描述-4"><a class="markdownIt-Anchor" href="#题目描述-4"></a> 题目描述</h3>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230520171345851.png" alt="image-20230520171345851"></p>
<h3 id="题解-4"><a class="markdownIt-Anchor" href="#题解-4"></a> 题解</h3>
<h4 id="我的思路-4"><a class="markdownIt-Anchor" href="#我的思路-4"></a> 我的思路</h4>
<p>本题要计算接雨水的数目，首先要明确按照行的顺序还是按照列的顺序来计算，并且只有当柱子位于缺口中的时候才能够接到雨水。按列计算可以使用暴力、动态规划、双指针方法，按行计算可以使用单调栈方法。</p>
<p><strong>暴力法</strong></p>
<p>可以想象每个柱子下面都有个接水的水桶，左右两边为桶的边。若柱子i能够接水，首先要保证柱子i处于缺口中，而柱子i左右两边的板子可以看成左右两边的最大元素left和right，那么柱子i能接的雨水数目num=min(pre_max, suf_max)-heights[i]。所有柱子的接水的累积量即为最终答案。</p>
<p>时间复杂度<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>O</mi><mo stretchy="false">(</mo><msup><mi>N</mi><mn>2</mn></msup><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">O(N^2)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.064108em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.02778em;">O</span><span class="mopen">(</span><span class="mord"><span class="mord mathdefault" style="margin-right:0.10903em;">N</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mclose">)</span></span></span></span></p>
<p><strong>前后缀分解法</strong></p>
<p>暴力解法的时间消耗主要在于内循环有大量的重复操作，这里可以预处理每个元素的后缀最大元素并存储在数组suf_max中，并且在枚举元素时计算元素i的前缀最大值pre_max。这样在枚举所有柱子的时候以O(1)的时间获得每个柱子的前缀最大值和后缀最大值。</p>
<p>时间复杂度O(N)</p>
<p><strong>双指针法</strong></p>
<p>对于前后缀分解时间复杂度上无法进一步优化，但是在空间复杂度上仍然可以进行优化。对于前后缀分解的空间上需要一个额外的数组来存储后缀最大值。</p>
<p>考虑对柱子i，已经知道i前缀的最大值pre_max，但不知道后缀最大值；柱子j已经知道j后缀的最大值，但是不知道j前缀的最大值。那么当i前缀最大值小于j的后缀最大值时，i能接水的最大高度为前缀最大值，那么i能接的雨水数目就知道了；反之当i的前缀最大值大于j的后缀最大值时，j能接水的最大值为后缀最大值，那么j能接的雨水数目就知道了。出于这样的考虑就可以使用双指针的方法来解决。</p>
<p>时间复杂度O(N)</p>
<p><strong>单调栈法</strong></p>
<p>该方法按照行的顺序计算接雨水的数目，如下图</p>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/stack.png" alt="stack"></p>
<p>前面说过柱子若想要接到雨水，必须位于缺口中，即对于任意柱子mid能接到雨水必须知道mid下一个更大元素位置r和上一个更大元素位置，那么mid上面行接到的雨水宽度w = r - l - 1，高度h = min(height[l], height[r]) - height[mid]，则mid上面行接到的雨水数目为w*h，而单调栈可以高效的计算元素下一个更大值，考虑下面三个问题：</p>
<p>1、栈中存储的元素是什么？</p>
<blockquote>
<p>由于需要寻找当前元素下一个更大元素和上一个更大元素的位置，所以栈中应当存储数组下标</p>
</blockquote>
<p>2、单调栈的顺序是什么？</p>
<blockquote>
<p>由于需要寻找下一个更大元素所以单调栈中元素从栈底到栈顶应当是递减的</p>
</blockquote>
<p>3、对于下述三种情况的处理方式？</p>
<ul>
<li>
<p>当前元素等于栈顶元素</p>
<blockquote>
<p>当前元素不是栈顶元素下一个更大元素，入栈</p>
</blockquote>
</li>
<li>
<p>当前元素小于栈顶元素</p>
<blockquote>
<p>当前元素不是栈顶元素下一个更大元素，入栈</p>
</blockquote>
</li>
<li>
<p>当前元素大于栈顶元素</p>
<blockquote>
<p>当前元素是栈顶元素下一个更大元素，栈顶出栈，mid = stack[hh]</p>
<p>此时mid下一个更大元素为当前元素i，上一个更大元素为栈底元素stack[hh]，</p>
<p>mid上面行接到的雨水数目为w*h，w和h的计算思路所示。</p>
<p>不断迭代该过程，直至栈顶元素大于等于当前元素为止，当前元素入栈</p>
</blockquote>
</li>
</ul>
<p>时间复杂度O(N)</p>
<h4 id="算法过程-3"><a class="markdownIt-Anchor" href="#算法过程-3"></a> 算法过程</h4>
<p><strong>暴力法</strong></p>
<ul>
<li>枚举所有柱子
<ul>
<li>对于元素i</li>
<li>计算元素i左边的最大元素left</li>
<li>计算元素i右边的最大元素right</li>
<li>元素i能够的接的雨水数为 num=min(pre_max, suf_max)-height[i]</li>
<li>更新答案</li>
</ul>
</li>
<li>枚举结束，返回答案</li>
</ul>
<p><strong>前后缀分解法</strong></p>
<ul>
<li>预处理所有柱子的后缀最大值，并存储在suf_max中</li>
<li>枚举所有柱子
<ul>
<li>对于柱子i</li>
<li>计算柱子i的前缀最大值pre_max = max(pre_max,height[i])</li>
<li>计算柱子能够接的雨水数num = min(pre_max, suf_max[i]) - height[i]</li>
<li>更新答案</li>
</ul>
</li>
<li>枚举结束，返回答案</li>
</ul>
<p><strong>双指针法</strong></p>
<ul>
<li>定义左右指针left=0和right=n-1</li>
<li>定义前后缀最大值pre_max，suf_max</li>
<li>相向双指针
<ul>
<li>计算left的前缀最大值pre_max和right的后缀最大值suf_max</li>
<li>若pre_max &gt; suf_max，计算right接的雨水量，right–</li>
<li>若pre_max &lt;= suf_max，计算left接的雨水量，left++</li>
</ul>
</li>
<li>两指针在最高点相遇，返回答案</li>
</ul>
<p><strong>单调栈</strong></p>
<ul>
<li>枚举所有元素</li>
<li>若当前元素小于等于栈顶元素，入栈</li>
<li>若当前元素大于栈顶元素
<ul>
<li>mid = stack[hh]</li>
<li>栈顶出栈hh–</li>
<li>w = i - stack[hh] - 1, h = min(height[i], height[stack[hh]]) - height[mid]</li>
<li>mid上一行接的雨水数为 w*h</li>
<li>更新答案，不断迭代该过程，直至当前元素小于等于栈顶元素为止</li>
</ul>
</li>
<li>当前元素入栈</li>
<li>枚举结束，返回结果</li>
</ul>
<h4 id="我的代码-4"><a class="markdownIt-Anchor" href="#我的代码-4"></a> 我的代码</h4>
<p><strong>暴力法</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 暴力枚举</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">trap</span><span class="params">(<span class="keyword">int</span>[] height)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>, n = height.length;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> pre_max = height[i];</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = i - <span class="number">1</span>; j &gt;= <span class="number">0</span>; j--)</span><br><span class="line">                pre_max = Math.max(pre_max, height[j]);</span><br><span class="line">            <span class="keyword">int</span> suf_max = height[i];</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = i + <span class="number">1</span>; j &lt; n; j++)</span><br><span class="line">                suf_max = Math.max(suf_max, height[j]);</span><br><span class="line">            ans += Math.min(pre_max, suf_max) - height[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>前后缀分解</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 前后缀分解</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">trap</span><span class="params">(<span class="keyword">int</span>[] height)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>, n = height.length;</span><br><span class="line">        <span class="keyword">int</span>[] suf_max = <span class="keyword">new</span> <span class="keyword">int</span>[n];</span><br><span class="line">        suf_max[n - <span class="number">1</span>] = height[n - <span class="number">1</span>];</span><br><span class="line">        <span class="comment">// 预处理：后缀最大值</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = n - <span class="number">2</span>; i &gt;= <span class="number">0</span>; i--)</span><br><span class="line">            suf_max[i] = Math.max(suf_max[i + <span class="number">1</span>], height[i]);</span><br><span class="line">        <span class="keyword">int</span> pre_max = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            pre_max = Math.max(pre_max, height[i]);</span><br><span class="line">            ans += Math.min(pre_max, suf_max[i]) - height[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>双指针</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 双指针</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">trap</span><span class="params">(<span class="keyword">int</span>[] height)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>, n = height.length;</span><br><span class="line">        <span class="keyword">int</span> pre_max = <span class="number">0</span>, suf_max = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> left = <span class="number">0</span>, right = n - <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span> (left &lt; right) &#123;</span><br><span class="line">            pre_max = Math.max(pre_max, height[left]);</span><br><span class="line">            suf_max = Math.max(suf_max, height[right]);</span><br><span class="line">            <span class="keyword">if</span>(pre_max &lt; suf_max)&#123;</span><br><span class="line">                ans += pre_max-height[left];</span><br><span class="line">                left++;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span>&#123;</span><br><span class="line">                ans += suf_max - height[right];</span><br><span class="line">                right--;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>单调栈</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 版本一，将上述三种情况分别用分支语句描述</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 单调栈</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">trap</span><span class="params">(<span class="keyword">int</span>[] height)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>, n = height.length;</span><br><span class="line">        <span class="keyword">int</span> hh = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span>[] stack = <span class="keyword">new</span> <span class="keyword">int</span>[n + <span class="number">10</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="comment">// 情况一和情况二</span></span><br><span class="line">            <span class="keyword">if</span> (height[i] &lt;= height[stack[hh]])</span><br><span class="line">                stack[++hh] = i;</span><br><span class="line">            <span class="keyword">else</span> &#123;<span class="comment">// 情况三</span></span><br><span class="line">                <span class="keyword">while</span> (hh != <span class="number">0</span> &amp;&amp; height[stack[hh]] &lt; height[i]) &#123;</span><br><span class="line">                    <span class="keyword">int</span> mid = stack[hh--];</span><br><span class="line">                    <span class="comment">// mid 有上一个更小元素</span></span><br><span class="line">                    <span class="keyword">if</span> (hh != <span class="number">0</span>) &#123;</span><br><span class="line">                        <span class="keyword">int</span> w = i - stack[hh] - <span class="number">1</span>;</span><br><span class="line">                        <span class="keyword">int</span> h = Math.min(height[i], height[stack[hh]]) - height[mid];</span><br><span class="line">                        ans += w * h;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                stack[++hh] = i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 版本二：版本一的缩减版</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 单调栈</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">trap</span><span class="params">(<span class="keyword">int</span>[] height)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>, n = height.length;</span><br><span class="line">        <span class="keyword">int</span> hh = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span>[] stack = <span class="keyword">new</span> <span class="keyword">int</span>[n + <span class="number">10</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="comment">// 情况三</span></span><br><span class="line">            <span class="keyword">while</span> (hh != <span class="number">0</span> &amp;&amp; height[stack[hh]] &lt; height[i]) &#123;</span><br><span class="line">                <span class="keyword">int</span> mid = stack[hh--];</span><br><span class="line">                <span class="comment">// mid上一个更小元素存在</span></span><br><span class="line">                <span class="keyword">if</span> (hh != <span class="number">0</span>) &#123;</span><br><span class="line">                    <span class="keyword">int</span> w = i - stack[hh] - <span class="number">1</span>;</span><br><span class="line">                    <span class="keyword">int</span> h = Math.min(height[i], height[stack[hh]]) - height[mid];</span><br><span class="line">                    ans += w * h;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 情况一和情况二</span></span><br><span class="line">            stack[++hh] = i;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="84柱状图中最大的矩形"><a class="markdownIt-Anchor" href="#84柱状图中最大的矩形"></a> 84.柱状图中最大的矩形</h2>
<h3 id="题目描述-5"><a class="markdownIt-Anchor" href="#题目描述-5"></a> 题目描述</h3>
<p><img src="https://raw.githubusercontent.com/zhengzp2019/Image/main/image-20230519173358221.png" alt="image-20230519173358221"></p>
<h3 id="题解-5"><a class="markdownIt-Anchor" href="#题解-5"></a> 题解</h3>
<h4 id="我的思路-5"><a class="markdownIt-Anchor" href="#我的思路-5"></a> 我的思路</h4>
<p><strong>暴力解法</strong></p>
<p>枚举所有柱子，对于柱子i所能勾勒出的最大矩形高度h为当前柱子高度height[i]，从位置i向右延伸到下一个更矮的柱子r为止，并且向左延伸到下一个更矮的柱子l为止，则宽度w=r-l-1。以当前柱子能勾勒出的最大矩形面积为h*w，枚举所有柱子计算最大值即可。时间复杂度O(N)。</p>
<p><strong>双指针法</strong></p>
<p>由于本题的数据规模是<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>1</mn><msup><mn>0</mn><mn>5</mn></msup></mrow><annotation encoding="application/x-tex">10^5</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">5</span></span></span></span></span></span></span></span></span></span></span>所以暴力的解法超时。暴力解法的内循环在寻找元素的下一个元素时有大量的冗余操作，考虑单调栈预处理元素的下一个更小元素并保存结果，然后在计算元素的前一个更小元素，并边计算便处理结果。时间复杂度为O(N)。</p>
<p><strong>单调栈法</strong></p>
<p>双指针法是先预处理元素的下一个更小元素，使用单调栈方法可以同时计算元素的前一个更小元素和下一个更小元素。考虑如下几个问题；</p>
<p>1、单调栈中存储的元素是什么？</p>
<p>因为需要的是当前元素的下一个更小元素的位置和上一更小元素的位置，所以栈中存储的元素是数组下标。</p>
<p>2、单调栈中元素的顺序是什么？</p>
<p>由于需要计算的是下一个更小元素的位置，所以栈的顺序应该是从栈底到栈顶单调递增的。</p>
<p>3、对于下面三种情况的处理方式</p>
<ul>
<li>
<p>当栈顶元素等于当前元素时</p>
<blockquote>
<p>当前元素不是栈顶元素下一个更小的值，入栈</p>
</blockquote>
</li>
<li>
<p>当栈顶元素小于当前元素时</p>
<blockquote>
<p>当前元素不是栈顶元素下一个更小的值，入栈</p>
</blockquote>
</li>
<li>
<p>当栈顶元素大于当前元素时</p>
<blockquote>
<p>当前元素是栈顶元素下一个更小的值，mid=stack[hh]，栈顶出栈</p>
<p>此时栈顶元素是mid上一个更小的元素，这里就找到了mid元素上一个更小元素stack[hh]和下一个更小元素i，处理答案，并迭代该过程，直至栈顶元素或等于当前元素为止</p>
</blockquote>
</li>
</ul>
<p>时间复杂度O(N)</p>
<h4 id="算法过程-4"><a class="markdownIt-Anchor" href="#算法过程-4"></a> 算法过程</h4>
<p><strong>暴力解法</strong></p>
<ul>
<li>枚举所有柱子
<ul>
<li>当前柱子i向左延伸计算下一个更矮的柱子位置l</li>
<li>当前柱子i向右延伸，计算下一个更矮的柱子位置r</li>
<li>当前柱子所能勾勒的最大矩形面积为 area = height[i]*(r-l-1)</li>
<li>更新答案ans = max(ans,area)</li>
</ul>
</li>
<li>枚举完所有的柱子，返回答案</li>
</ul>
<p><strong>双指针法</strong></p>
<ul>
<li>计算元素的下一个更小元素，并保存在数组next_greater中</li>
<li>枚举所有元素
<ul>
<li>若栈不空或者当前元素i小于栈顶元素，弹栈</li>
<li>若栈空，说明当前元素没有前一个更小元素，即l=-1</li>
<li>若栈不空，则说明前一个更小元素的位置l=stack[hh]</li>
<li>计算当前柱子能勾勒的最大矩形面积</li>
<li>更新答案</li>
</ul>
</li>
<li>枚举完所有的柱子，返回答案</li>
</ul>
<p><strong>单调栈法</strong></p>
<ul>
<li>
<p>枚举所有元素</p>
<ul>
<li>
<p>若当前元素大于或等于栈顶元素，当前元素入栈</p>
</li>
<li>
<p>若当前元素小于栈顶元素，令mid=stack[hh]，栈顶元素出栈，此时对于</p>
<p>mid来说，上一个更小元素的为止为stack[hh]，下一个更小元素的位置为i，</p>
<p>处理答案，不断迭代直至条件不满足</p>
</li>
<li>
<p>当前元素入栈</p>
</li>
</ul>
</li>
<li>
<p>枚举结束</p>
</li>
<li>
<p>若栈不空，处理栈内元素，由于栈内元素是单调递增的，他们不存在下一个更小元素，也就是他们下一个更小元素的位置为n，所有要单独处理栈内所有元素，直至栈为空</p>
<blockquote>
<p>注：这里相对于常规的求下一个元素更小值要注意如下两点问题</p>
<ol>
<li>若数组元素是单调递减的，则任意元素都无上一个最小值，即上一个最小值的位置为-1，按照上述方法计算mid上一个最小值时，栈都是空的。这里可以将hh从1开始计数，当hh=0时栈空，并且令stack[0]==-1，这样即使栈空时，stack[hh]仍然是mid上一个元素的最小值，可以保证代码的一致性</li>
<li>若数组元素时单调递增的，则任意元素都无下一个最小值，即下一个最小值的位置为n，最终的结果表现为这些元素存储在栈中等待处理，所以最后要单独处理栈中的元素</li>
</ol>
</blockquote>
</li>
</ul>
<h4 id="我的代码-5"><a class="markdownIt-Anchor" href="#我的代码-5"></a> 我的代码</h4>
<p><strong>暴力解法</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">largestRectangleArea</span><span class="params">(<span class="keyword">int</span>[] heights)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>, n = heights.length;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> l = i;<span class="comment">// l指向i左边下一个更小的元素</span></span><br><span class="line">            <span class="keyword">while</span> (l &gt;= <span class="number">0</span> &amp;&amp; heights[l] &gt;= heights[i])</span><br><span class="line">                l--;</span><br><span class="line">            <span class="keyword">int</span> r = i; <span class="comment">// r指向i右边下一个更小的元素</span></span><br><span class="line">            <span class="keyword">while</span> (r &lt; n &amp;&amp; heights[r] &gt;= heights[i])</span><br><span class="line">                r++;</span><br><span class="line">            <span class="keyword">int</span> area = heights[i] * (r - l - <span class="number">1</span>);</span><br><span class="line">            ans = Math.max(ans, area);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>双指针法</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">largestRectangleArea</span><span class="params">(<span class="keyword">int</span>[] heights)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>, n = heights.length;</span><br><span class="line">        <span class="keyword">int</span> hh = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span>[] stack = <span class="keyword">new</span> <span class="keyword">int</span>[n + <span class="number">10</span>];</span><br><span class="line">        <span class="keyword">int</span>[] next_greater = <span class="keyword">new</span> <span class="keyword">int</span>[n];</span><br><span class="line">        Arrays.fill(next_greater, n);</span><br><span class="line">        <span class="comment">// 预处理元素的下一个更小元素的位置</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">while</span> (hh != <span class="number">0</span> &amp;&amp; heights[stack[hh]] &gt; heights[i]) &#123;</span><br><span class="line">                <span class="keyword">int</span> id = stack[hh--];</span><br><span class="line">                next_greater[id] = i;</span><br><span class="line">            &#125;</span><br><span class="line">            stack[++hh] = i;</span><br><span class="line">        &#125;</span><br><span class="line">        hh = <span class="number">0</span>;</span><br><span class="line">        stack[hh] = -<span class="number">1</span>;</span><br><span class="line">        <span class="comment">// 计算元素的上一个更小元素的位置</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">while</span> (hh != <span class="number">0</span> &amp;&amp; heights[stack[hh]] &gt;= heights[i])</span><br><span class="line">                hh--;</span><br><span class="line">            <span class="keyword">int</span> area = heights[i] * (next_greater[i] - stack[hh] - <span class="number">1</span>);</span><br><span class="line">            ans = Math.max(ans, area);</span><br><span class="line">            stack[++hh] = i;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>单调栈法</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 版本一：将上述三种情况使用分支语句处理</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">largestRectangleArea</span><span class="params">(<span class="keyword">int</span>[] heights)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>, n = heights.length;</span><br><span class="line">        <span class="keyword">int</span> hh = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span>[] stack = <span class="keyword">new</span> <span class="keyword">int</span>[n + <span class="number">10</span>];</span><br><span class="line">        stack[hh] = -<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="comment">// 情况一和二：栈不空，且栈顶元素小于等于当前元素</span></span><br><span class="line">            <span class="keyword">if</span> (hh != <span class="number">0</span> &amp;&amp; heights[stack[hh]] &lt; heights[i])</span><br><span class="line">                stack[++hh] = i;</span><br><span class="line">            <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">// 情况三：栈不空且栈顶元素大于当前元素，循环处理答案</span></span><br><span class="line">                <span class="keyword">while</span> (hh != <span class="number">0</span> &amp;&amp; heights[stack[hh]] &gt; heights[i]) &#123;</span><br><span class="line">                    <span class="keyword">int</span> mid = stack[hh--];</span><br><span class="line">                    <span class="keyword">int</span> area = heights[mid] * (i - stack[hh] - <span class="number">1</span>);</span><br><span class="line">                    ans = Math.max(ans, area);</span><br><span class="line">                &#125;</span><br><span class="line">                stack[++hh] = i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 单独处理栈内元素</span></span><br><span class="line">        <span class="keyword">int</span> r = n;</span><br><span class="line">        <span class="keyword">while</span> (hh != <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">int</span> mid = stack[hh--];</span><br><span class="line">            <span class="keyword">int</span> area = heights[mid] * (r - stack[hh] - <span class="number">1</span>);</span><br><span class="line">            ans = Math.max(ans, area);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 版本二：版本一的简洁版</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">largestRectangleArea</span><span class="params">(<span class="keyword">int</span>[] heights)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>, n = heights.length;</span><br><span class="line">        <span class="keyword">int</span> hh = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span>[] stack = <span class="keyword">new</span> <span class="keyword">int</span>[n + <span class="number">10</span>];</span><br><span class="line">        stack[hh] = -<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="comment">// 情况三：栈不空且栈顶元素大于当前元素，循环处理答案</span></span><br><span class="line">            <span class="keyword">while</span> (hh != <span class="number">0</span> &amp;&amp; heights[stack[hh]] &gt; heights[i]) &#123;</span><br><span class="line">                <span class="keyword">int</span> mid = stack[hh--];</span><br><span class="line">                <span class="keyword">int</span> area = heights[mid] * (i - stack[hh] - <span class="number">1</span>);</span><br><span class="line">                ans = Math.max(ans, area);</span><br><span class="line">            &#125;</span><br><span class="line">            stack[++hh] = i;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 单独处理栈内元素</span></span><br><span class="line">        <span class="keyword">int</span> r = n;</span><br><span class="line">        <span class="keyword">while</span> (hh != <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">int</span> mid = stack[hh--];</span><br><span class="line">            <span class="keyword">int</span> area = heights[mid] * (r - stack[hh] - <span class="number">1</span>);</span><br><span class="line">            ans = Math.max(ans, area);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2023/05/09/%E9%98%9F%E5%88%97%E7%9B%B8%E5%85%B3%E9%A2%98%E7%9B%AE/" rel="prev" title="队列相关题目">
      <i class="fa fa-chevron-left"></i> 队列相关题目
    </a></div>
      <div class="post-nav-item">
    <a href="/2023/05/25/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%81%8D%E5%8E%86%E6%96%B9%E5%BC%8F/" rel="next" title="二叉树的遍历方式">
      二叉树的遍历方式 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%B1%87%E6%80%BB"><span class="nav-text"> 题目汇总</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#739-%E6%AF%8F%E6%97%A5%E6%B8%A9%E5%BA%A6"><span class="nav-text"> 739. 每日温度</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%AE%97%E6%B3%95%E8%BF%87%E7%A8%8B"><span class="nav-text"> 算法过程</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#496%E4%B8%8B%E4%B8%80%E4%B8%AA%E6%9B%B4%E5%A4%A7%E5%85%83%E7%B4%A0i"><span class="nav-text"> 496.下一个更大元素I</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-2"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3-2"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF-2"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%AE%97%E6%B3%95%E8%BF%87%E7%A8%8B-2"><span class="nav-text"> 算法过程</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81-2"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#503%E4%B8%8B%E4%B8%80%E4%B8%AA%E6%9B%B4%E5%A4%A7%E5%85%83%E7%B4%A0ii"><span class="nav-text"> 503.下一个更大元素II</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-3"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3-3"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF-3"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81-3"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#42-%E6%8E%A5%E9%9B%A8%E6%B0%B4"><span class="nav-text"> 42. 接雨水</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-4"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3-4"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF-4"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%AE%97%E6%B3%95%E8%BF%87%E7%A8%8B-3"><span class="nav-text"> 算法过程</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81-4"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#84%E6%9F%B1%E7%8A%B6%E5%9B%BE%E4%B8%AD%E6%9C%80%E5%A4%A7%E7%9A%84%E7%9F%A9%E5%BD%A2"><span class="nav-text"> 84.柱状图中最大的矩形</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-5"><span class="nav-text"> 题目描述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E8%A7%A3-5"><span class="nav-text"> 题解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E6%80%9D%E8%B7%AF-5"><span class="nav-text"> 我的思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%AE%97%E6%B3%95%E8%BF%87%E7%A8%8B-4"><span class="nav-text"> 算法过程</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E4%BB%A3%E7%A0%81-5"><span class="nav-text"> 我的代码</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="dpbirder"
      src="/images/boy.jpg">
  <p class="site-author-name" itemprop="name">dpbirder</p>
  <div class="site-description" itemprop="description">目前研一就读于西安电子科技大学</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">51</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="sidebar-button motion-element">
    <a onclick="Chatra('openChat', true);"><i class="fa fa-comment"></i>
    Chat
  </a>
  </div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/zhengzp2019" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhengzp2019" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://mail.qq.com/" title="E-Mail → https:&#x2F;&#x2F;mail.qq.com&#x2F;" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        
  <div class="languages">
    <label class="lang-select-label">
      <i class="fa fa-language"></i>
      <span>简体中文</span>
      <i class="fa fa-angle-up" aria-hidden="true"></i>
    </label>
    <select class="lang-select" data-canonical="">
      
        <option value="zh-CN" data-href="/2023/05/19/%E5%8D%95%E8%B0%83%E6%A0%88%E7%9B%B8%E5%85%B3%E9%A2%98%E7%9B%AE/" selected="">
          简体中文
        </option>
      
        <option value="en" data-href="/en/2023/05/19/%E5%8D%95%E8%B0%83%E6%A0%88%E7%9B%B8%E5%85%B3%E9%A2%98%E7%9B%AE/" selected="">
          English
        </option>
      
    </select>
  </div>

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">dpbirder</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">225k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">3:25</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>


  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>




  <script>
    (function(d, w, c) {
      w.ChatraID = 'Z8KH5JTPhxGC84tKb';
      var s = d.createElement('script');
      w[c] = w[c] || function() {
        (w[c].q = w[c].q || []).push(arguments);
      };
      s.async = true;
      s.src = 'https://call.chatra.io/chatra.js';
      if (d.head) d.head.appendChild(s);
    })(document, window, 'Chatra');
  </script>









  

  

  

</body>
</html>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/love.js"></script>
